//
//  CarSunscribeListVC.m
//  New_Runway
//
//  Created by mack on 2018/3/20.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarSunscribeListVC.h"
#import "NRWCarListsModel.h"
#import "NRWCarCarListCell.h"
#import "BuyCarDetailVC.h"

@interface CarSunscribeListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (assign, nonatomic) NSInteger pageNo;
@property (nonatomic, strong) NSMutableArray *subscribeArray;
@end

@implementation CarSunscribeListVC
-(NSMutableArray *)subscribeArray{
    if (!_subscribeArray) {
        _subscribeArray = [[NSMutableArray alloc] init];
    }
    return _subscribeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"订阅车源";
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    self.pageNo = 1;
    [self loadSubscribeCar];
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWCarCarListCell" bundle:nil] forCellReuseIdentifier:@"NRWCarCarListCell"];
    self.tableview.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downPullRefresh)];
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upPullLoadMore)];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(self.navH);
        make.bottom.equalTo(weakSelf.view);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
        self.tableview.estimatedRowHeight = 0;
        self.tableview.estimatedSectionHeaderHeight = 0;
        self.tableview.estimatedSectionFooterHeight = 0;
        
    } else {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.automaticallyAdjustsScrollViewInsets=NO;
        
    }
   
    
}
-(void)downPullRefresh{
    self.pageNo = 1;
    [self loadSubscribeCar];
}
-(void)upPullLoadMore{
    self.pageNo += 1;
    [self loadSubscribeCar];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 110;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return self.subscribeArray.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    NRWCarCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarCarListCell"];
    cell.isBuyCar = YES;
    cell.carModel = self.subscribeArray[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

        Result *rt = self.subscribeArray[indexPath.row];
        BuyCarDetailVC *detailVC = [[BuyCarDetailVC alloc] init];
        detailVC.carID = rt.carId;
        detailVC.carModel =rt;
        detailVC.isHistory = NO;
        [self.navigationController pushViewController:detailVC animated:YES];

    
}
-(void)loadSubscribeCar{
    NSDictionary *params = @{@"marketId":[NRWCarUser sharedUser].marketId,
                             @"shopId":[NRWCarUser sharedUser].shopId ?[NRWCarUser sharedUser].shopId : @"" ,
                             @"pageIndex":@(self.pageNo),
                             @"pageSize":@"10",
                             @"carSaleState":@(20)
                             };
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",Api,subscribeCarLisrApi] refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if (self.pageNo == 1) {
            [self.subscribeArray removeAllObjects];
        }
        NSArray *noticeArr = response[@"result"];
        for (NSDictionary *dic in noticeArr) {
            Result *rt = [Result mj_objectWithKeyValues:dic];
            [self.subscribeArray addObject:rt];
        }
        
        [self.tableview reloadData];
        
        [self endRefresh];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

        [self endRefresh];
        
    }];
    
    
}
-(void)endRefresh{
    
    [self.tableview.mj_header endRefreshing];
    [self.tableview.mj_footer endRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
